/*
 * @Author: zhangyang
 * @Date: 2021-11-14 17:47:16
 * @LastEditTime: 2021-11-14 18:10:58
 * @Description: 
 */
const { Service }  = require('egg');

module.exports = class UserService extends Service {
  async getAll() {
    const { ctx } = this;
    const users = await ctx.model.User.findAll();
    return users;
  }

  async add(name, age) {
    const { ctx } = this;
    const user = await ctx.model.User.create({ name, age });
    return user;
  }

  async getById(id) {
    const { ctx } = this;
    const user = await ctx.model.User.findByPk(id);
    return user;
  }

  async update(id, name, age) {
    const user = await this.getById(id);
    if (user) {
      await user.update({ name, age });
      return user;
    } else {
      return '查无此人';
    }
  }

  async delById(id) {
    const user = await this.getById(id);
    if (user) {
      await user.destroy();
      return '删除成功';
    } else {
      return '查无此人';
    }
  }
}